//支付功能
import api from '../utils/api'
//常量
var app = getApp();
function generateOrder() {
  var that = this;
  //统一支付
  wx.showToast({
    title: '正在处理订单...',
    icon: 'loading',
    duration: 10000,
    mask: true
  })
  //var login = wx.getStorageSync('login');
  var orderidStorage = wx.getStorageSync('orderid');
  setTimeout(function () {
    // wx.request({
    // 	url: `${app.globalData.host}/mpbase/wxapp/wxpay/mp_id/${app.globalData.mp_id}/id/` + orderidStorage,
    // 	method: 'post',
    // 	data: {
    // 		// session_3rd: login.session_3rd
    // 	},
    // 	success: function (res) {
    // 		var pay = res.data;
    // 		//发起支付
    // 		//支付
    // 		payFn(pay);
    // 	},
    // })
    api.request('/order/pay', 'POST', {
      'order_no': orderidStorage
    }, (res) => {
      console.log(res.data)
      var pay = res.data;
      //发起支付
      //支付
      payFn(pay);
    });
  }, 2000)
}
/* 支付   */
function payFn(param) {
  wx.requestPayment({
    timeStamp: param.timestamp,
    nonceStr: param.nonceStr,
    package: param.package,
    signType: param.signType,
    paySign: param.paySign,
    success: function (res) {
      wx.showModal({
        title: '提示',
        content: '支付成功',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/payment/orderdail/orderdail',
            });
            console.log(res);
          }
        }
      })
      // wx.navigateBack({
      // delta: 1, // 回退前 delta(默认为1) 页面
      // success: function (res) {
      //     wx.showToast({
      //         title: '支付成功',
      //         icon: 'success',
      //         duration: 2000
      //     })
      // },
      // })
    },
    fail: function (res) {
      // fail
      console.log("支付失败")
      console.log(res)
      wx.showModal({
        title: '提示',
        content: '支付失败',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/payment/orderdail/orderdail',
            });
          }
        }
      })
      return
    },
    complete: function () {
    }
  })
}
module.exports = {
  generateOrder: generateOrder
}